//
//  RecommendedFeedbackController.m
//  SSSAllRollAgency
//
//  Created by Darker on 2017/3/15.
//  Copyright © 2017年 Darker. All rights reserved.
//

#import "RecommendedFeedbackController.h"


@interface RecommendedFeedbackController ()<UITextViewDelegate>

@property(strong, nonatomic)ZDTextView * recommendTextView;
@property(strong, nonatomic)UILabel * numLabel;

@end

@implementation RecommendedFeedbackController
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
    self.title = @"建议反馈";
    //去掉透明后导航栏下边的黑边
    //    [self.navigationController.navigationBar setShadowImage:[[UIImage alloc] init]];
    self.navigationController.navigationBar.hidden = NO;
    self.view.backgroundColor = [UIColor colorWithHex:@"F9F9FA"];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    _recommendTextView = [[ZDTextView alloc]initWithFrame:CGRectMake(WidthIos6CompeteWithCurrent(11), 64 + HeigthIos6CompeteWithCurrent(16), KSCREEN_Width - WidthIos6CompeteWithCurrent(11)-WidthIos6CompeteWithCurrent(13), HeigthIos6CompeteWithCurrent(230))];
    _recommendTextView.contentInset = UIEdgeInsetsMake(-64, 0, 0, 0);
    _recommendTextView.layer.cornerRadius = 8;
    _recommendTextView.layer.masksToBounds = YES;
    _recommendTextView.backgroundColor = [UIColor clearColor];
    _recommendTextView.layer.borderWidth = 0.5;
    _recommendTextView.layer.borderColor = [UIColor colorWithHex:@"BBBBBB"].CGColor;
    _recommendTextView.delegate = self;
    _recommendTextView.font = [SSSFont systemFontOfSize:15];
    _recommendTextView.textColor = [UIColor grayColor];
    _recommendTextView.placeholderColor = [UIColor colorWithHex:@"BBBBBB"];
    _recommendTextView.textAlignment = NSTextAlignmentLeft;
    _recommendTextView.placeholder = @"请留下你宝贵的意见吧";
    [self.view addSubview:_recommendTextView];
    
    
    _numLabel = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(_recommendTextView.frame)-100, CGRectGetMaxY(_recommendTextView.frame)-26, 80, 21)];
    _numLabel.textAlignment = NSTextAlignmentRight;
    _numLabel.text = @"0/200";
    _numLabel.textColor = [UIColor colorWithHex:@"BBBBBB"];
    //_numLabel.backgroundColor = [UIColor colorWithHex:@"FFFFFF"];
    [self.view addSubview:_numLabel];

    
    //确定取消按钮
    //确定按钮
    UIButton *sureBtn = [[UIButton alloc]init];
    [sureBtn setTitle:@"确 定" forState:UIControlStateNormal];
    [sureBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [sureBtn setBackgroundColor:[UIColor colorWithHex:@"46B77D"]];
    sureBtn.titleLabel.font = [SSSFont systemFontOfSize:20];
    [sureBtn addTarget:self action:@selector(sureBtnClick) forControlEvents:UIControlEventTouchDown];
    [self.view addSubview:sureBtn];
    sureBtn.sd_layout
    .topSpaceToView(_recommendTextView,HeigthIos6CompeteWithCurrent(40))
    .leftSpaceToView(self.view,WidthIos6CompeteWithCurrent(43))
    .rightSpaceToView(self.view,WidthIos6CompeteWithCurrent(43))
    .heightIs(HeigthIos6CompeteWithCurrent(51));
    sureBtn.layer.cornerRadius = 5;
    sureBtn.layer.masksToBounds = YES;
    
    //取消按钮
    UIButton *cancelBtn = [[UIButton alloc]init];
    [cancelBtn setTitle:@"取 消" forState:UIControlStateNormal];
    [cancelBtn setTitleColor:[UIColor colorWithHex:@"46B77D"] forState:UIControlStateNormal];
    [cancelBtn setBackgroundColor:[UIColor clearColor]];
    cancelBtn.titleLabel.font = [SSSFont systemFontOfSize:20];
    [cancelBtn addTarget:self action:@selector(cancelBtnClick) forControlEvents:UIControlEventTouchDown];
    [self.view addSubview:cancelBtn];
    cancelBtn.sd_layout
    .topSpaceToView(sureBtn,HeigthIos6CompeteWithCurrent(20))
    .leftEqualToView(sureBtn)
    .rightEqualToView(sureBtn)
    .heightIs(HeigthIos6CompeteWithCurrent(51));
    cancelBtn.layer.cornerRadius = 5;
    cancelBtn.layer.masksToBounds = YES;
    cancelBtn.layer.borderColor = [UIColor colorWithHex:@"46B77D"].CGColor;
    cancelBtn.layer.borderWidth = 0.5;
    
}
#pragma mark textField的字数限制

//在这个地方计算输入的字数
- (void)textViewDidChange:(UITextView *)textView
{
    NSInteger wordCount = textView.text.length;
    self.numLabel.text = [NSString stringWithFormat:@"%ld/200",(long)wordCount];
    [self wordLimit:textView];
}

#pragma mark 超过300字不能输入
-(BOOL)wordLimit:(UITextView *)text{
    if (text.text.length < 200) {
        NSLog(@"%ld",(unsigned long)text.text.length);
        //self.textView.editable = YES;
    }
    else{
        //self.textView.editable = NO;
        
    }
    return false;
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    
    [_recommendTextView resignFirstResponder];
}

- (void)sureBtnClick{
    
    
    [_recommendTextView resignFirstResponder];
    if ([_recommendTextView.text isEqualToString:@""]) {
        
        [QMUITips showError:@"请输入您的宝贵意见或建议" inView:self.view hideAfterDelay:2];
        return;
        
    }else{
        
        NSMutableDictionary *params = [NSMutableDictionary dictionary];
        params[@"content"] = _recommendTextView.text;
        [[XHRequestManager manager] postRequest:kConfigPath(kRecommende) params:params success:^(id  _Nonnull responseObj) {
            
            if ([responseObj[@"status"] intValue] == 0) {
                
                [QMUITips showSucceed:@"提交成功" inView:self.view hideAfterDelay:2];
                [self performSelector:@selector(backSuperController) withObject:nil afterDelay:2.0f];
            }else{
                
                [QMUITips showError:responseObj[@"message"] inView:self.view hideAfterDelay:2];
            }
            
        } failure:^(NSError * _Nonnull error) {
            
            [QMUITips showError:error.localizedDescription inView:self.view hideAfterDelay:2];
        }];
    }

}
- (void)cancelBtnClick{
    
     [self.navigationController popViewControllerAnimated:YES];
}
- (void)backSuperController{
    
    [self.navigationController popViewControllerAnimated:YES];
}
@end
